// components/pickfilter.js
const app = getApp()
const watch = require("../utils/watch.js");
import areaListData from '../config/area.js'
let areaList = [
  {
    name: '选择',
    value: 0,
    city: []
  }
]
areaList = areaList.concat(areaListData.list)
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showmenu: {
      type: Boolean,
      value: true
    },
    province: {
      type: Number,
      value: 0
    },
    city: {
      type: Number,
      value: 0
    },
    area: {
      type: Number,
      value: 0
    },
    category_id: {
      type: Number,
      value: undefined,
      observer: function (new_val, old_val, path) {
        console.log("category_id值变化了...", arguments);
        if (old_val > 0 && new_val != old_val) {
          this.data.searchParam.category_id = new_val
          this.data.searchParam.page = 1
          this.setData({
            searchParam: this.data.searchParam
          })
          this.getTaskList(this.data.searchParam)
        }
      }
    },
    order: {
      type: Number,
      value: 0
    },
    page: {
      type: Number,
      value: 0,
      observer: function (new_val, old_val, path) {
        console.log("page值变化了...", arguments);
        if (old_val > 0 && new_val > old_val )
        {
          this.data.searchParam.page = new_val
          this.setData({
            searchParam: this.data.searchParam
          })
          this.getTaskList(this.data.searchParam)
        }
      }
    },
    category_name: {
      type: String,
      value: ''
    },
    keyword: {
      type: String,
      value: ''
    },
    autoload: {
      type: Boolean,
      value: false
    },
    // 翻页
    paganator: {
      type: Boolean,
      value: false
    },
    searchParam: {
      type: Object,
      value: {}
    }
  },

  lifetimes: {
    attached() {
      const data = this.data
      setTimeout(() => {
        this.data.searchParam.province = data.province
        this.data.searchParam.city = data.city
        this.data.searchParam.area = data.area
        this.data.searchParam.category_id = data.category_id
        this.data.searchParam.order = data.order
        this.data.searchParam.page = data.page
        this.data.searchParam.keyword = data.keyword
        this.setData({
          searchParam: this.data.searchParam
        })
        console.log('[ATTACHED]', this.data) 
        if (this.data.autoload) {
          this.getTaskList(this.data.searchParam)
        }
      },200)
      this.renderAreaData()
      this.getJobCateData()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    array: [
      [],
      [],
      []
    ],
    index: [0, 0, 0],
    order: 0,
    jobCateData: [],
    selectedJobCateData: undefined,
    areaData: {
    },
    task: [],
    lastQueryString: {}
    // searchParam: {
    //   province: this.data.province,
    //   city: this.data.city,
    //   area: this.data.area,
    //   category_id: this.data.category_id,
    //   order: this.data.order,
    //   page: this.data.page,
    // }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    setParam: function(data, callback)
    {
      this.setData({
        searchParam: Object.assign(data,this.data.searchParam)
      })
      if( callback && callback instanceof Function )
      {
        callback(this.data.searchParam)
      }
    },
    bindPickerChange: function (e) {
      console.log("[条件切换]", e)
      var changeType = e.currentTarget.dataset.type || 'region';
      var value = e.detail.value
      if ('region' == changeType) {
        var areaData = {
          province: areaList[value[0]],
          city: value[1] > 0 ? areaList[value[0]].city[value[1] - 1] : undefined,
          area: value[1] > 0 && value[2] > 0 ? areaList[value[0]].city[value[1] - 1].area[value[2] - 1] : undefined,
        }
        this.setData({
          areaData: areaData
        })
        console.log('[地区选择结果]', areaData)
        this.data.searchParam.province = areaData.province ? areaData.province.value : 0
        this.data.searchParam.city = areaData.city ? areaData.city.value : 0
        this.data.searchParam.area = areaData.area ? areaData.area.value : 0
        this.setData({
          searchParam: this.data.searchParam
        })
      } else if ('jobcate' == changeType) {
        this.data.searchParam.category_id = this.data.jobCateData[value] ? this.data.jobCateData[value].id : 0
        this.setData({
          selecteJobCateData: value || undefined,
          searchParam: this.data.searchParam
        })
      } else {
        this.data.searchParam.order = this.data.searchParam.order > 0 ? 0 : 1
        this.setData({
          searchParam: this.data.searchParam
        })
      }
      this.setData({
        task: []
      })
      this.getTaskList(this.data.searchParam)
    },
    bindAreaColumnChange: function (e) {
      console.log("[区域切换]", areaList)
      this.data.index[e.detail.column] = e.detail.value;
      if (e.detail.column < 2) {
        if (e.detail.column == 0) {
          if (e.detail.value == 0 )
          {
            this.data.array[1] = []
          }else{
            const city = [{ value: 0, name: '选择', city: [] }]
            this.data.array[1] = city.concat(areaList[e.detail.value].city)
          }
          this.data.array[2] = [];
          this.data.index[1] = 0;
          this.data.index[2] = 0;
          this.setData({
            array: this.data.array,
            index: this.data.index
          })
        } else if (e.detail.column == 1) {
          if( e.detail.value == 0 )
          {            
            this.data.array[2] = []
          }else{
            const area = [{ value: 0, name: '选择', city: [] }]
            this.data.array[2] = area.concat(areaList[this.data.index[0]].city[e.detail.value - 1].area)
          }
          this.data.index[2] = 0;          
          this.setData({
            array: this.data.array,
            index: this.data.index
          })
        }
      }
      console.log("[区域切换结果]", e, areaList, this.data.index)
    },    
    /*
    * 获取行业数据
    */
    getJobCateData: function () {
      console.log('aewfafafaf')
      app.http.$ajax({
        url: '/user/jobCate',
      }, {
        success: (res) => {
          // console.log(res)
          var data = [{
            id: 0,
            name: '选择'
          }]
          this.setData({
            jobCateData: data.concat( res.data )
          })
        }
      }, {
          loading: this.data.paganator ? false :'数据加载中...',
      })
    },    
    renderAreaData: function () {
      this.data.array[0] = areaList
      this.data.array[1] = this.data.array[2] = [];
      this.setData({
        array: this.data.array
      })
      console.log('[RENDER_DATA]', this.data)
    },
    getTaskList: function ({
      province = 0,
      city = 0,
      area = 0,
      category_id = 0,
      order = 1,
      page = 1,
      keyword = ''
    } = {}) {
      if( this.data.lastQueryString ){
        for( var i in this.data.searchParam)
        { 
          console.log('[参数发生变化]', i, this.data.searchParam[i], this.data.lastQueryString[i], i != 'page' && this.data.searchParam[i] != this.data.lastQueryString[i])
          if (i != 'page' && this.data.searchParam[i] != this.data.lastQueryString[i] ){
            
            this.data.searchParam.page = this.data.page = 1
            this.setData({
              page: this.data.page,
              searchParam: this.data.searchParam,
              task: []
            })
            this._ReceiveData()
            break;
          }
        }
      }

      this.triggerEvent('isLoading', {
        searchParam: this.data.searchParam,
        isLoading: true
      })
      
      app.http.$ajax({
        url: '/index/getTaskList',
        data: {
          province: province,
          city: city,
          area: area,
          category_id: category_id,
          order: order,
          keyword: keyword,
          p: page
        }
      }, {
          success: (res) => {            
            if (this.data.paganator)
            {
              this.setData({
                task: this.data.task.concat(res.data),
                lastQueryString: Object.assign({},this.data.searchParam)
              })
            }else{
              this.setData({
                task: res.data,
                lastQueryString: this.data.searchParam
              })
            }            
            this._ReceiveData()
          },
          complete: () => {
            this.triggerEvent('isLoading', {
              searchParam: this.data.searchParam,
              isLoading: false
            })
          }
        }, {
          loading: this.data.paganator ? false :'加载中'
        })
    },
    _ReceiveData: function(){
      this.triggerEvent('ReceiveData', {
        searchParam: this.data.searchParam,
        data: this.data.task
      })
    }
  }
})
